package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zmn.common.utils.MethodSignUtils;
import com.zmn.manager.common.cat.http.MonitorHttpsUtils;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 类描述：新单同步到呼叫系统
 * @author heciqi
 * @date 2019/11/5 19:38
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OrderCallSyncListener {
	private static final Logger logger = LoggerFactory.getLogger(OrderChangeListener.class);

	/**
	 * 订单同步（呼叫中心）
	 */
	@Value("${call.order.sync.url:http://dev-cc.xiujiadian.com/open/order/sync}")
	String CALL_ORDER_SYNC_URL;
	/**
	 * 呼叫信息签名key
	 */
	@Value("${call.info.sign.key:867D0B5FBEBFEF9818DF9F39E87EAA9E}")
	String SIGN_KEY;

	@AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_WORK_SYNC_NEW_CALL)
	public void orderChange(String message) {
		logger.debug("#mq#ordercallsynclistener#recive message:{}", message);
		try {

			Map<String, String> params = JSONObject.parseObject(message, new TypeReference<Map<String, String>>() {
			});
			params.put("sign", MethodSignUtils.sign(params, SIGN_KEY));
			logger.debug("#callsync# 呼叫同步接口请求 {}, {}", CALL_ORDER_SYNC_URL, JSON.toJSONString(params));
			String res = MonitorHttpsUtils.postForm(CALL_ORDER_SYNC_URL, params);
			logger.info("#call#callsync# 呼叫信息接口响应 {}", res);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
}
